package com.qf.stu.application.controller;

import com.qf.stu.application.entity.Stu;
import com.qf.stu.application.feign.ClsRemote;
import com.qf.stu.application.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/stu")
//会根据远程配置中心，实时刷新配置
@RefreshScope
public class StuController {
    @Value("${com.name}")
    private String name;
    @Autowired
    private ClsRemote clsRemote;

    @Autowired
    private StuService service;

    @RequestMapping("/get")
    public String get(Integer sid){
        System.out.println("查询学生信息！" + sid + " 配置：" + name);
        String name = switch (sid) {
            case 1 -> "张三";
            case 2 -> "李四";
            default -> "查无此人";
        };

        //远程调用班级服务 根据学生姓名获取所在班级
        String clsName = clsRemote.get(name);

        return name + "-" + clsName;
    }

    /**
     * 新增学生
     * @return
     */
    @RequestMapping("/insert")
    public String insert(){
        service.save(new Stu().setName("小明"));
        return "success";
    }
}
